package com.nd.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.BrandMapper;
import com.nd.pojo.bean.Brand;
import com.nd.pojo.bean.BrandExample;
import com.nd.pojo.bo.wx.BrandListWXBo;
import com.nd.pojo.vo.wx.BrandDetailVo;
import com.nd.pojo.vo.wx.BrandListBeanWXVo;
import com.nd.pojo.vo.wx.BrandListWXVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : PengFei Zhang
 * @Data :2021/6/8  16:12
 * @Description :
 */
//品牌 zpf
@Service
public class BrandWXServiceImpl implements BrandWXService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandListWXVo selectBrandToList(BrandListWXBo brandListWXBo) {
        //拿出所有的品牌和其相关信息
        BrandExample brandExample = new BrandExample();
        // BrandExample.Criteria criteria = brandExample.createCriteria();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        //分页
        PageHelper.startPage(brandListWXBo.getPage(), brandListWXBo.getSize());
        //将得到的信息传入到BrandLisetBeanWXVo的一个集合
        ArrayList<BrandListBeanWXVo> brandListBeanWXVos = new ArrayList<>();
        for (Brand brand : brands) {
            BrandListBeanWXVo brandListBeanWXVo = new BrandListBeanWXVo();
            brandListBeanWXVo.setId(brand.getId());
            brandListBeanWXVo.setName(brand.getName());
            brandListBeanWXVo.setDesc(brand.getDesc());
            brandListBeanWXVo.setPicUrl(brand.getPicUrl());
            double floorPrice = brand.getFloorPrice().doubleValue();
            brandListBeanWXVo.setFloorPrice(floorPrice);
            brandListBeanWXVos.add(brandListBeanWXVo);
        }
        //可以获得分页信息
        PageInfo<Brand> PageInfo = new PageInfo<>(brands);
        //获得所有条数信息
        long total = PageInfo.getTotal();

        BrandListWXVo brandListWXVo = new BrandListWXVo();
        brandListWXVo.setTotalPages(PageInfo.getPages());
        brandListWXVo.setBrandList(brandListBeanWXVos);

        return brandListWXVo;
    }

    //品牌详情
    @Override
    public BrandDetailVo selectBrandDetail(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        BrandDetailVo brandDetailVo = new BrandDetailVo();
        brandDetailVo.setId(brand.getId());
        brandDetailVo.setName(brand.getName());
        brandDetailVo.setDesc(brand.getDesc());
        brandDetailVo.setPicUrl(brand.getPicUrl());
        brandDetailVo.setSortOrder(brand.getSortOrder());
        brandDetailVo.setFloorPrice(brand.getFloorPrice().intValue());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = formatter.format(brand.getAddTime());
        brandDetailVo.setAddTime(addTime);
        String updateTime = formatter.format(brand.getUpdateTime());
        brandDetailVo.setUpdateTime(updateTime);
        brandDetailVo.setDeleted(brand.getDeleted());
        return brandDetailVo;
    }
}
